package by.demchenko.sample.webapp.page.admin;

import by.demchenko.sample.datamodel.Course;
import by.demchenko.sample.datamodel.Course_;
import by.demchenko.sample.services.CourseService;
import org.apache.wicket.authroles.authorization.strategies.role.annotations.AuthorizeInstantiation;
import org.apache.wicket.extensions.markup.html.repeater.data.sort.SortOrder;
import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigator;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import javax.inject.Inject;
import javax.persistence.metamodel.SingularAttribute;
import java.util.Iterator;

@AuthorizeInstantiation("ADMIN")
public class CoursesPage extends BaseAdminPage {

    @Inject
    CourseService courseService;

    @Override
    protected void onInitialize() {
        super.onInitialize();
        add(new Label("headerTitle", getPageTitle()));
        add(new Label("subtitle", getString("label.courses")));

        CourseDataProvider courseDataProvider = new CourseDataProvider();

        DataView<Course> dataView = new DataView<Course>("coursesList", courseDataProvider) {
            @Override
            protected void populateItem(Item<Course> item) {
                final Course course = item.getModelObject();
                item.add(new Label("name", course.getName()));
                item.add(new Label("teacher", course.getTeacher().getFullName()));
                item.add(new Label("section", course.getSection().getName()));
                item.add(new Label("description", course.getDescription()));

                item.add(new Link("editbutton") {
                    @Override
                    public void onClick() {
                        setResponsePage(new CourseEditPage(course));
                    }
                });
                item.add(new Link("deletebutton") {
                    @Override
                    public void onClick()
                    {
                        courseService.delete(course.getId());
                        setResponsePage(new CoursesPage());
                    }
                });
            }
        };

        dataView.setItemsPerPage(5);
        add(dataView);

        add(new PagingNavigator("navigator", dataView));

        add(new Link("newbutton") {
            @Override
            public void onClick() {
                setResponsePage(new CourseEditPage(new Course()));
            }
        });
    }

    private class CourseDataProvider extends SortableDataProvider<Course, SingularAttribute<Course, ?>> {

        public CourseDataProvider() {
            super();
            setSort(Course_.name, SortOrder.ASCENDING);
        }

        @Override
        public Iterator<? extends Course> iterator(long l, long l1) {
            SingularAttribute<Course, ?> sortParam = getSort().getProperty();
            SortOrder propertySortOrder = getSortState().getPropertySortOrder(sortParam);
            boolean ascending = SortOrder.ASCENDING.equals(propertySortOrder);
            return courseService.getAll(sortParam, ascending, (int)l, (int)l1, true).iterator();

        }

        @Override
        public long size() {
            return courseService.getCount();
        }

        @Override
        public IModel<Course> model(Course course) {
            return new CompoundPropertyModel<>(course);
        }
    }

    private IModel<String> getPageTitle() {
        return new Model<String>(getClass().getSimpleName());
    }
}
